package com.qqt.csr.order.service.statemachine.condition.impl;

import com.qqt.csr.common.exception.StatusCode;
import com.qqt.csr.common.utils.ServiceAssert;
import com.qqt.csr.order.entity.WorkOrder;
import com.qqt.csr.order.enums.WorkOrderEventEnum;
import com.qqt.csr.order.service.statemachine.condition.ISubCondition;
import com.qqt.csr.order.service.statemachine.context.WorkOrderContext;
import org.springframework.stereotype.Component;

@Component
public class DeletedWorkOrderSubCondition implements ISubCondition {
    @Override
    public boolean matches(WorkOrderEventEnum event) {
        return WorkOrderEventEnum.DELETED.equals(event);
    }
    @Override
    public boolean isSatisfied(WorkOrderContext context) {
        WorkOrder fromOrder = context.getFromOrder();

        if (fromOrder.isAllocated()) {
            ServiceAssert.isTrue(fromOrder.getCreatorId().equals(context.getOperatorId()) || fromOrder.getDesigneeId().equals(context.getOperatorId()), StatusCode.Common.ILLEGAL.getCode(), "无权限操作");
        }else {
            ServiceAssert.isTrue(fromOrder.getCreatorId().equals(context.getOperatorId()), StatusCode.Common.ILLEGAL.getCode(), "无权限操作");
        }
        return true;
    }
}
